/* zTree.js 模糊查询树形导航 */

var zTreeObj;
// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
var setting = {
    check: {
        autoCheckTrigger: true,
        chkboxType: { "Y": "ps", "N": "ps" },
        chkStyle: "checkbox",
        enable: true,
        nocheckInherit: true,
        chkDisabledInherit: true,
        radioType: "level"
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    view: {
        nameIsHTML: true, //允许name支持html				
        selectedMulti: false
    },
    edit: {
        enable: false,
        editNameSelectAll: false
    }
};
// zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
var zNodes =[
    { id:1, pId:0, name:"aaa", open:true,dataId:101},
    { id:2, pId:0, name:"bbb", open:true,dataId:102},
    { id:3, pId:0, name:"ccc", open:true,dataId:103},
    { id:4, pId:0, name:"ddd", open:true,dataId:104},
    { id:5, pId:0, name:"eee", open:true,dataId:105},
    { id:6, pId:1, name:"aaabbb", open:true,dataId:106},
    { id:7, pId:1, name:"aaabbbccc", open:true,dataId:107},
    { id:8, pId:1, name:"aaabbbcccddd", open:true,dataId:108},
    { id:9, pId:2, name:"bbbccc", open:true,dataId:109},
    { id:10, pId:2, name:"bbbccceee", open:true,dataId:110},
    { id:11, pId:3, name:"bbbdddeeefff", open:true,dataId:111},
];

//重新定位树形菜单的位置
function setUlStyle(){
    var tree_ipt = $("#treeDemo_keyword");
    $("#treeDemo").css({left:tree_ipt.offset().left-1,top:tree_ipt.offset().top+tree_ipt.height()-2}).width(tree_ipt.outerWidth()-11);
}

//封装树形数据
function setTreeDataFn(data){
    var tnode = [];
    for(var i=0;i<data.length;i++){
        //console.log("一级：",data[i].text);
        tnode.push({
            id:data[i].id,
            pId:data[i].id,
            name:data[i].text,
            open:true,
            dataId:data[i].id
        });
        for(var o=0;o<data[i].children.length;o++){
            //console.log("二级：",data[i].children[o].text);
            tnode.push({
                id:data[i].children[o].id,
                pId:data[i].id,
                name:data[i].children[o].text,
                open:true,
                dataId:data[i].children[o].id
            });
            for(var s=0;s<data[i].children[o].children.length;s++){
                //console.log("三级：",data[i].children[o].children[s].text);
                tnode.push({
                    id:data[i].children[o].children[s].id,
                    pId:data[i].children[o].id,
                    name:data[i].children[o].children[s].text,
                    open:true,
                    dataId:data[i].children[o].children[s].id
                });
            }
        }
    }
    return tnode;
}

//初始化树形结构
function initTreeFn(zNodes){
    //初始化化树形导航
    zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);

    //初始化模糊搜索方法
    fuzzySearch('treeDemo','#treeDemo_keyword',null,true);

    //重新定位树形菜单的位置
    setUlStyle();
    $(window).resize(function () {
        setUlStyle();
    });

    //绑定整个dom
    $(document).click(function(e){
        if ( $((e.target || e.srcElement)).closest("#treeDemo_keyword").length == 0 && $((e.target || e.srcElement)).closest("#treeDemo").length == 0){
            $("#treeDemo").hide();
        }
    })
}

//获取已选中的树形结构数据
function getTreeDataId(data){
    var tnode = [];
    for(var i=0;i<data.length;i++){
        tnode.push(data[i].id);
    }
    return tnode;
}

$(document).ready(function () {

    //初始化本地存储
    window.localStorage.removeItem("zTreeData1"); 

    //预先加载，获取数据
    $.getJSON("./data/data.json",function (data) {
        //保存到本地存储
    window.localStorage.setItem("zTreeData1",JSON.stringify(data));
    });

    //绑定input显示模糊结果框
    $("#treeDemo_keyword").click(function () {

        var td = window.localStorage.getItem("zTreeData1");

        if(!td){

            //获取数据
            $.getJSON("./data/data.json",function (data) {
                //保存到本地存储
                window.localStorage.setItem("zTreeData1",JSON.stringify(data));

                if($("#treeDemo").find("li").length==0){
                    //渲染是户型结构
                    initTreeFn(setTreeDataFn(data));
                    $("#treeDemo").show();
                }
            });
        }else{
            //渲染是户型结构
            if($("#treeDemo").find("li").length==0) {
                initTreeFn(setTreeDataFn(JSON.parse(td)));
            }
            $("#treeDemo").show();
        }
    })

    //绑定提交按钮
    $("#submitBtn").click(function(){
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo")
        var nodes = treeObj.getChangeCheckedNodes();
        console.log(nodes);
        var treeids = getTreeDataId(nodes);
        console.log(treeids);
    });

});